home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / NUHBTD (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  4.7 KB  |  308 lines

  1. package sun.net.www;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.PrintStream;
  6.  
  7. public class MessageHeader {
  8.    private String[] keys;
  9.    private String[] values;
  10.    private int nkeys;
  11.  
  12.    public MessageHeader() {
  13.       this.grow();
  14.    }
  15.  
  16.    public MessageHeader(InputStream is) throws IOException {
  17.       this.parseHeader(is);
  18.    }
  19.  
  20.    public void add(String k, String v) {
  21.       this.grow();
  22.       this.keys[this.nkeys] = k;
  23.       this.values[this.nkeys] = v;
  24.       ++this.nkeys;
  25.    }
  26.  
  27.    public static String canonicalID(String id) {
  28.       if (id == null) {
  29.          return "";
  30.       } else {
  31.          int st = 0;
  32.          int len = id.length();
  33.  
  34.          boolean substr;
  35.          char c;
  36.          for(substr = false; st < len && ((c = id.charAt(st)) == '<' || c <= ' '); substr = true) {
  37.             ++st;
  38.          }
  39.  
  40.          while(st < len && ((c = id.charAt(len - 1)) == '>' || c <= ' ')) {
  41.             --len;
  42.             substr = true;
  43.          }
  44.  
  45.          return substr ? id.substring(st, len) : id;
  46.       }
  47.    }
  48.  
  49.    public String findNextValue(String k, String v) {
  50.       boolean foundV = false;
  51.       if (k == null) {
  52.          int i = this.nkeys;
  53.  
  54.          while(true) {
  55.             --i;
  56.             if (i < 0) {
  57.                break;
  58.             }
  59.  
  60.             if (this.keys[i] == null) {
  61.                if (foundV) {
  62.                   return this.values[i];
  63.                }
  64.  
  65.                if (this.values[i] == v) {
  66.                   foundV = true;
  67.                }
  68.             }
  69.          }
  70.       } else {
  71.          int i = this.nkeys;
  72.  
  73.          while(true) {
  74.             --i;
  75.             if (i < 0) {
  76.                break;
  77.             }
  78.  
  79.             if (k.equalsIgnoreCase(this.keys[i])) {
  80.                if (foundV) {
  81.                   return this.values[i];
  82.                }
  83.  
  84.                if (this.values[i] == v) {
  85.                   foundV = true;
  86.                }
  87.             }
  88.          }
  89.       }
  90.  
  91.       return null;
  92.    }
  93.  
  94.    public String findValue(String k) {
  95.       if (k == null) {
  96.          int i = this.nkeys;
  97.  
  98.          while(true) {
  99.             --i;
  100.             if (i < 0) {
  101.                break;
  102.             }
  103.  
  104.             if (this.keys[i] == null) {
  105.                return this.values[i];
  106.             }
  107.          }
  108.       } else {
  109.          int i = this.nkeys;
  110.  
  111.          while(true) {
  112.             --i;
  113.             if (i < 0) {
  114.                break;
  115.             }
  116.  
  117.             if (k.equalsIgnoreCase(this.keys[i])) {
  118.                return this.values[i];
  119.             }
  120.          }
  121.       }
  122.  
  123.       return null;
  124.    }
  125.  
  126.    public String getKey(int n) {
  127.       return n >= 0 && n < this.nkeys ? this.keys[n] : null;
  128.    }
  129.  
  130.    public String getValue(int n) {
  131.       return n >= 0 && n < this.nkeys ? this.values[n] : null;
  132.    }
  133.  
  134.    private void grow() {
  135.       if (this.keys == null || this.nkeys >= this.keys.length) {
  136.          String[] nk = new String[this.nkeys + 4];
  137.          String[] nv = new String[this.nkeys + 4];
  138.          if (this.keys != null) {
  139.             System.arraycopy(this.keys, 0, nk, 0, this.nkeys);
  140.          }
  141.  
  142.          if (this.values != null) {
  143.             System.arraycopy(this.values, 0, nv, 0, this.nkeys);
  144.          }
  145.  
  146.          this.keys = nk;
  147.          this.values = nv;
  148.       }
  149.  
  150.    }
  151.  
  152.    public void parseHeader(InputStream is) throws IOException {
  153.       this.nkeys = 0;
  154.       if (is != null) {
  155.          char[] s = new char[10];
  156.  
  157.          String v;
  158.          String k;
  159.          for(int firstc = is.read(); firstc != 10 && firstc != 13 && firstc >= 0; this.add(k, v)) {
  160.             int len = 0;
  161.             int keyend = -1;
  162.             boolean inKey = firstc > 32;
  163.             s[len++] = (char)firstc;
  164.  
  165.             label106:
  166.             while(true) {
  167.                int c;
  168.                if ((c = is.read()) < 0) {
  169.                   firstc = -1;
  170.                   break;
  171.                }
  172.  
  173.                switch (c) {
  174.                   case 9:
  175.                      c = 32;
  176.                   case 32:
  177.                      inKey = false;
  178.                      break;
  179.                   case 10:
  180.                   case 13:
  181.                      firstc = is.read();
  182.                      if (c == 13 && firstc == 10) {
  183.                         firstc = is.read();
  184.                         if (firstc == 13) {
  185.                            firstc = is.read();
  186.                         }
  187.                      }
  188.  
  189.                      if (firstc == 10 || firstc == 13 || firstc > 32) {
  190.                         break label106;
  191.                      }
  192.  
  193.                      c = 32;
  194.                      break;
  195.                   case 58:
  196.                      if (inKey && len > 0) {
  197.                         keyend = len;
  198.                      }
  199.  
  200.                      inKey = false;
  201.                }
  202.  
  203.                if (len >= s.length) {
  204.                   char[] ns = new char[s.length * 2];
  205.                   System.arraycopy(s, 0, ns, 0, len);
  206.                   s = ns;
  207.                }
  208.  
  209.                s[len++] = (char)c;
  210.             }
  211.  
  212.             while(len > 0 && s[len - 1] <= ' ') {
  213.                --len;
  214.             }
  215.  
  216.             if (keyend <= 0) {
  217.                k = null;
  218.                keyend = 0;
  219.             } else {
  220.                k = String.copyValueOf(s, 0, keyend);
  221.                if (keyend < len && s[keyend] == ':') {
  222.                   ++keyend;
  223.                }
  224.  
  225.                while(keyend < len && s[keyend] <= ' ') {
  226.                   ++keyend;
  227.                }
  228.             }
  229.  
  230.             if (keyend >= len) {
  231.                v = new String();
  232.             } else {
  233.                v = String.copyValueOf(s, keyend, len - keyend);
  234.             }
  235.          }
  236.  
  237.       }
  238.    }
  239.  
  240.    public void prepend(String k, String v) {
  241.       this.grow();
  242.  
  243.       for(int i = this.nkeys; i > 0; --i) {
  244.          this.keys[i] = this.keys[i - 1];
  245.          this.values[i] = this.values[i - 1];
  246.       }
  247.  
  248.       this.keys[0] = k;
  249.       this.values[0] = v;
  250.       ++this.nkeys;
  251.    }
  252.  
  253.    public void print(PrintStream p) {
  254.       for(int i = 0; i < this.nkeys; ++i) {
  255.          if (this.keys[i] != null) {
  256.             p.print(this.keys[i] + (this.values[i] != null ? ": " + this.values[i] : "") + "\r\n");
  257.          }
  258.       }
  259.  
  260.       p.print("\r\n");
  261.       p.flush();
  262.    }
  263.  
  264.    public void set(int i, String k, String v) {
  265.       this.grow();
  266.       if (i >= 0) {
  267.          if (i > this.nkeys) {
  268.             this.add(k, v);
  269.          } else {
  270.             this.keys[i] = k;
  271.             this.values[i] = v;
  272.          }
  273.  
  274.       }
  275.    }
  276.  
  277.    public void set(String k, String v) {
  278.       int i = this.nkeys;
  279.  
  280.       do {
  281.          --i;
  282.          if (i < 0) {
  283.             this.add(k, v);
  284.             return;
  285.          }
  286.       } while(!k.equalsIgnoreCase(this.keys[i]));
  287.  
  288.       this.values[i] = v;
  289.    }
  290.  
  291.    public void setIfNotSet(String k, String v) {
  292.       if (this.findValue(k) == null) {
  293.          this.add(k, v);
  294.       }
  295.  
  296.    }
  297.  
  298.    public String toString() {
  299.       String result = super.toString();
  300.  
  301.       for(int i = 0; i < this.keys.length; ++i) {
  302.          result = result + "{" + this.keys[i] + ": " + this.values[i] + "}";
  303.       }
  304.  
  305.       return result;
  306.    }
  307. }
  308.